import { defineStore } from "pinia";
import axios from "axios"
import { nanoid } from "nanoid";
import { reactive } from "vue";

// store的组合式写法
export const useTalkStore = defineStore('loveTalk', () => {
  const talkList = reactive([
    { id: '1', content: '今天有点怪，哪里怪，怪好看的！' }
  ])
  // getLoveTalk函数相当于action
  async function getLoveTalk() {
    axios.get('https://api.uomg.com/api/rand.qinghua?format=json').then(res => {
      talkList.push({
        id: nanoid(),
        content: res.data.content
      })
      console.log(19, talkList)
    })
  }
  return { talkList, getLoveTalk }
})

// store的选项式写法
/* export const useTalkStore = defineStore("loveTalk", {
  state: () => {
    return {
      talkList: [
        { id: '1', content: '今天有点怪，哪里怪，怪好看的！' }
      ]
    };
  },
  actions: {
    getLoveTalk() {
      axios.get('https://api.uomg.com/api/rand.qinghua?format=json').then(res => {
        this.talkList.push({
          id: nanoid(),
          content: res.data.content
        })
        console.log(this.talkList);
      })
    }
  },
}); */